<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="项目名称:">
        <el-input v-model="queryParams.projectName" size="small" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:project:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:project:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:project:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:project:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目代码" align="center" prop="id" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目单位" align="center" prop="owner" />
      <el-table-column label="项目类型" align="center" prop="projectType" />
      <el-table-column label="建设性质" align="center" prop="projectNature" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="联系人电话" align="center" prop="contactPhone" />
      <el-table-column label="项目预算" align="center" prop="estimatedAmount" />
      <el-table-column label="项目行业" align="center" prop="industry" />
      <el-table-column label="拟建区域" align="center" prop="area" />
      <el-table-column label="拟建地点" align="center" prop="address" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="预计开工时间" align="center" prop="expectedStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计完成时间" align="center" prop="expectedEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已开工" align="center" prop="isStarted" />
      <el-table-column label="入库时间" align="center" prop="stockInTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stockInTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库状态" align="center" prop="stockStatus" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['basic:project:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['basic:project:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="projectFormDialog.title" :visible.sync="projectFormDialog.visible" :close-on-click-modal="false" :before-close="resetProjectFormDialog" width="500px" append-to-body>
      <el-form ref="projectForm" :model="projectFormData" :rules="projectFormRules" label-width="80px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="projectFormData.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目单位" prop="owner">
          <el-input v-model="projectFormData.owner" placeholder="请输入项目单位" />
        </el-form-item>
        <el-form-item label="建设性质" prop="projectNature">
          <el-input v-model="projectFormData.projectNature" placeholder="请输入建设性质" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="projectFormData.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactPhone">
          <el-input v-model="projectFormData.contactPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="项目预算" prop="estimatedAmount">
          <el-input v-model="projectFormData.estimatedAmount" placeholder="请输入项目预算" />
        </el-form-item>
        <el-form-item label="项目行业" prop="industry">
          <el-input v-model="projectFormData.industry" placeholder="请输入项目行业" />
        </el-form-item>
        <el-form-item label="拟建区域" prop="area">
          <el-input v-model="projectFormData.area" placeholder="请输入拟建区域" />
        </el-form-item>
        <el-form-item label="拟建地点" prop="address">
          <el-input v-model="projectFormData.address" placeholder="请输入拟建地点" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="projectFormData.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="projectFormData.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="预计开工时间" prop="expectedStartTime">
          <el-date-picker v-model="projectFormData.expectedStartTime" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择预计开工时间" clearable />
        </el-form-item>
        <el-form-item label="预计完成时间" prop="expectedEndTime">
          <el-date-picker v-model="projectFormData.expectedEndTime" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择预计完成时间" clearable />
        </el-form-item>
        <el-form-item label="是否已开工" prop="isStarted">
          <el-input v-model="projectFormData.isStarted" placeholder="请输入是否已开工" />
        </el-form-item>
        <el-form-item label="入库时间" prop="stockInTime">
          <el-date-picker v-model="projectFormData.stockInTime" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择入库时间" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="projectFormData.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker v-model="projectFormData.createDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择创建时间" clearable />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateDate">
          <el-date-picker v-model="projectFormData.updateDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择更新时间" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmProjectFormDialog">确 定</el-button>
        <el-button @click="resetProjectFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ProjectApi from '@/api/basic/ProjectApi'

const defaultProjectFormData = {
  projectName: null,
  owner: null,
  projectType: null,
  projectNature: null,
  contact: null,
  contactPhone: null,
  estimatedAmount: null,
  industry: null,
  area: null,
  address: null,
  longitude: null,
  latitude: null,
  expectedStartTime: null,
  expectedEndTime: null,
  isStarted: null,
  stockInTime: null,
  stockStatus: '0',
  createDate: null,
  updateDate: null
}
export default {
  name: 'ProjectList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        projectName: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      projectFormDialog: {
        title: '',
        visible: false
      },
      projectFormData: Object.assign({}, defaultProjectFormData),
      projectFormRules: {
        projectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openProjectFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ProjectApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openProjectFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      ProjectApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ProjectApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openProjectFormDialog(id) {
      this.projectFormDialog.visible = true
      if (id) {
        this.projectFormDialog.title = '项目-修改'
        ProjectApi.get(id).then(response => {
          this.projectFormData = {
            ...this.projectFormData,
            ...response.data
          }
        })
      } else {
        this.projectFormDialog.title = '项目-添加'
      }
    },
    confirmProjectFormDialog() {
      this.$refs['projectForm'].validate(valid => {
        if (valid) {
          if (this.projectFormData.id) {
            ProjectApi.edit(this.projectFormData).then(response => {
              this.$message.success('修改成功')
              this.resetProjectFormDialog()
              this.handleQuery()
            })
          } else {
            ProjectApi.add(this.projectFormData).then(response => {
              this.$message.success('添加成功')
              this.resetProjectFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetProjectFormDialog() {
      this.projectFormDialog = {
        title: '',
        visible: false
      }
      this.projectFormData = Object.assign({}, defaultProjectFormData)
      this.resetForm('projectForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
